<template>
  <a-table
    :columns="columns"
    :dataSource="dataSource"
    :loading="loading"
    :pagination="false"
    bordered
    rowKey="id"
    size="middle"
  >
    <template slot="htmlSlot" slot-scope="text">
      <div v-html="text"></div>
    </template>

    <template slot="imgSlot" slot-scope="text">
      <div style="font-size: 12px; font-style: italic">
        <span v-if="!text">无图片</span>
        <img :src="getImgView(text)" alt="" style="max-width: 80px; height: 25px" v-else/>
      </div>
    </template>

    <template slot="fileSlot" slot-scope="text">
      <span style="font-size: 12px; font-style: italic" v-if="!text">无文件</span>
      <a-button @click="downloadFile(text)" ghost icon="download" size="small" type="primary" v-else>
        <span>下载</span>
      </a-button>
    </template>
  </a-table>
</template>

<script>
  import { getAction } from '@api/manage'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'

  export default {
    name: 'StockAdjustApprovalDetailSubTable',
    mixins: [JeecgListMixin],
    props: {
      record: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        description: '库存调整明细表内嵌列表',
        disableMixinCreated: true,
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '商品',
            align: 'center',
            dataIndex: 'goodsId_dictText'
          },
          {
            title: '规格',
            align: 'center',
            dataIndex: 'goodsSpec_dictText'
          },
          {
            title: '调整数量',
            align: 'center',
            dataIndex: 'adjustStock'
          }
        ],
        url: {
          listByMainId: '/storage/stockAdjustApproval/queryStockAdjustApprovalDetailByMainId'
        }
      }
    },
    watch: {
      record: {
        immediate: true,
        handler(val) {
          if (val != null && val.adjustType === 0) {
            if (val.status === 1) {
              this.columns.push(
                ...[
                  {
                    title: '调整前数量',
                    dataIndex: 'beforeStock',
                    align: 'center'
                  },
                  {
                    title: '调整后数量',
                    dataIndex: 'afterStock',
                    align: 'center'
                  }
                ]
              )
            }
            this.columns.unshift({
              title: '调整仓位',
              align: 'center',
              dataIndex: 'fromSpaceId_dictText'
            })
            this.loadData(val)
          } else {
            this.columns.unshift(
              {
                title: '调整前仓位',
                align: 'center',
                dataIndex: 'fromSpaceId_dictText'
              },
              {
                title: '调整后仓位',
                align: 'center',
                dataIndex: 'toSpaceId_dictText'
              }
            )
            this.loadData(val)
          }
        }
      }
    },
    methods: {
      loadData(record) {
        this.loading = true
        this.dataSource = []
        getAction(this.url.listByMainId, {
          id: record.id
        })
          .then((res) => {
            if (res.success) {
              this.dataSource = res.result.records
            }
          })
          .finally(() => {
            this.loading = false
          })
      }
    }
  }
</script>

<style scoped>
</style>
